<?php Yii::app()->clientScript->registerCssFile('css/InvoiceReport.css');?>
<div id="invoice-report">
    <div id="create-date">
        <?php echo date('Y-m-d'); ?>
        <br/>
        <?php echo date('H:i:s'); ?>
    </div>

    <div id="title">
        Invoice Report        
    </div>
    <div class="details">
        <div class="start-date">Start Date <?php echo $model->startDate; ?></div>
        <div class="end-date">End Date <?php echo $model->endDate; ?></div>
        <div class="type">Type <?php echo $model->type==''?'All':$model->type; ?></div>
        <div class="department">Department <?php echo $model->department==''?'All':  SalesDepartments::getText($model->department); ?></div>
    </div>
    <div id="content">
        <?php
        $filename = 'csv/'.'invoice-report-temp-'.$model->startDate.'-'.$model->endDate.'-.csv';
        $file = fopen($filename,'w');
        $table=new CI_Table();
        $table->set_template(array(
            'table_open'=>'<table class="zeebra">'
        ));
        $table->set_heading('Type','InvoiceNo','Reference','Date','Total','Job No','Contract No','Contract Start',' Contract End','Bill Type','Location');
       fputcsv($file,array(
	       'Type','InvoiceNo','Reference','Date','Total','Job No','Contract No','Contract Start',' Contract End','Bill Type','Location'
       ));
        $total=0;
        foreach($invoiceList as $invoice){
            /* @var $invoice Invoice */
	        if($invoice->agreementno){
		        $startDate = $invoice->contract->contractstartdate;
		        $endDate = $invoice->contract->contractenddate;
		        $billType =$invoice->contract-> getBillTypeText();
		        $contractLink =  CHtml::link($invoice->agreementno,array('contracts/update','contractid'=>$invoice->agreementno));
	        } else {
		        $contractLink = $startDate=$endDate=$billType = 'N/A';
	        }

            $reference=$invoice->quoteorg!=NULL?$invoice->quoteorg : $invoice->salesordersorg;
            $table->add_row(
                    $invoice->invoicetype,
                    CHtml::link($invoice->invoice,array('invoices/update','invoice'=>$invoice->invoice)),
                    CHtml::link($reference,array('invoices/update','invoice'=>$reference)),
                    $invoice->invoicedate,
                    $invoice->getAmountWithDiscount(),
		            $invoice->jobnumber,
	                $contractLink,
		            $startDate,
	                $endDate,
		            $billType,
	                $invoice->getLocation()->locname
                    );
	        fputcsv($file,array(
		        $invoice->invoicetype,
				$invoice->invoice,
		        $reference,
		        $invoice->invoicedate,
		        $invoice->getAmountWithDiscount(),
		        $invoice->jobnumber,
		        $invoice->agreementno,
		        $startDate,
		        $endDate,
		        $billType,
		        $invoice->getLocation()->locname
	        ));
            $total+=$invoice->getAmountWithDiscount();
        }
        fclose($file);
		echo CHtml::link('Export in CSV',array('/site/download','filename'=>urlencode($filename)));
        echo $table->generate();
        ?>
       
    </div>
    <?php if($model->department!=NULL):?>
    <br/>
        <hr/>
        <div id="total">Total <?php echo number_format($total,2);?></div>
    <?php endif;?>
</div>
